//
//  ABTestable.swift
//  SwiftCode
//
//  Created by yuhus on 2023/6/12.
//

import Foundation

@propertyWrapper
public struct ABTestable<T: ABValue> {
  private var key: String
  /// 默认值
  private var value: T
  
  public var wrappedValue: T {
    get {
      let provider: ABTestProvider?
      switch ABTestConfiguration.environment {
      case .local: provider = ABTestConfiguration.local
      case .online: provider = ABTestConfiguration.online
      }
      let currentValue = provider?.getABValue(key: key, default: value) ?? value
      ABTestElement.allValues[key] = currentValue
      return currentValue
    }
    set {}
  }
  
  init(wrappedValue: T, key: String, desc: String) {
    self.key = key
    self.value = wrappedValue
    ABTestElement.allElements.append(.init(key: key, defaultValue: wrappedValue, desc: desc))
  }
}

